package com.hyc.myvideo.ui.activity.home;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.flyco.tablayout.SlidingTabLayout;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.ui.base.BaseFragment;
import com.hyc.myvideo.ui.fragment.home.TrainingCatalogFragment;
import com.hyc.myvideo.ui.fragment.home.TrainingRecordFragment;
import com.hyc.myvideo.widget.TrainingAddDialog;
import java.util.ArrayList;
import java.util.List;
import timber.log.Timber;

/**
 * 修炼
 * Created by dfeads on 2017/7/13.
 */

public class TrainingActivity extends BaseFActivity {

  private final String[] mTitles = { "修炼目录", "修炼记录" };
  private List<BaseFragment> fragments = new ArrayList<>();
  @BindView(R.id.activity_training_stl) SlidingTabLayout mTable;
  @BindView(R.id.activity_training_vp) ViewPager mPager;
  @BindView(R.id.fragment_teacher_head) LinearLayout mLHeader;
  @BindView(R.id.fragment_teacher_head_line)TextView mLine;
  /** 天数 */
  @BindView(R.id.fragment_teacher_day) TextView mDay;
  /** 时长 */
  @BindView(R.id.fragment_teacher_time) TextView mTime;
  /** 添加 */
  @BindView(R.id.head_right_img) ImageView mAdd;
  private MyPagerAdapter mAdapter;
  private TrainingAddDialog trainingAddDialog;
  private TrainingCatalogFragment trainingCatalogFragment;
  private TrainingRecordFragment trainingRecordFragment;

  @Override public int getContentViewId() {
    return R.layout.activity_training;
  }

  @Override protected void initView() {
    setLeftBtnClick(true, getStr(R.string.training));
    mAdd.setImageResource(R.mipmap.training_add);
    mAdd.setVisibility(View.VISIBLE);
    mLHeader.setVisibility(View.VISIBLE);
    mLine.setVisibility(View.VISIBLE);

    trainingCatalogFragment = new TrainingCatalogFragment();
    trainingRecordFragment = new TrainingRecordFragment();
    fragments.add(trainingCatalogFragment);
    fragments.add(trainingRecordFragment);
    mAdapter = new MyPagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mAdapter);
    mTable.setViewPager(mPager);

    trainingAddDialog = new TrainingAddDialog(this);
    trainingAddDialog.setTrainingAddListener(new TrainingAddDialog.OnTrainingAddListener() {
      @Override public void getCatalog(String catalog) {
        api.setPracticeCatalog(userInfo.getUser_id(), catalog);
      }
    });

    mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
      }

      @Override public void onPageSelected(int position) {
        Timber.e("==onPageSelected===" + position);
        switch (position) {
          case 0:
            mAdd.setVisibility(View.VISIBLE);
            break;
          case 1:
            mAdd.setVisibility(View.GONE);
            break;
        }
      }

      @Override public void onPageScrollStateChanged(int state) {
      }
    });
  }

  @Override protected void initData() {

  }

  @Override protected void oNext(String resulte, String method) {
    BaseResult<String> base =
        JSONObject.parseObject(resulte, new TypeReference<BaseResult<String>>() {
        });
    if (base.isCode()) {
      trainingCatalogFragment.getPracticeList();
    }
    Toaster.show(base.getMsg());
  }

  @Override protected void oError() {

  }

  private void addCatalog() {
    trainingAddDialog.show();
  }

  @OnClick(R.id.head_right_img) public void onViewClicked() {
    addCatalog();
  }

  private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
      super(fm);
    }

    @Override public Fragment getItem(int position) {
      return fragments.get(position);
    }

    @Override public int getCount() {
      return mTitles.length;
    }

    @Override public CharSequence getPageTitle(int position) {
      return mTitles[position];
    }
  }
}
